package com.lo.service.impl;

import com.lo.common.JsonResult;
import com.lo.config.CommentConfig;
import com.lo.dao.CommentMapper;
import com.lo.dao.TeacherMapper;
import com.lo.dao.UserMapper;
import com.lo.entity.Comment;
import com.lo.entity.Teacher;
import com.lo.entity.User;
import com.lo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public JsonResult selectCourseComments(Long cid) {
        List<Comment> comments = commentMapper.selectCommentsByCid(cid);
        if (comments.size()<=0){
            return new JsonResult(0,"评论为空",null);
        }
        return new JsonResult(0,"查询评论成功",CommentConfig.processComments(comments));
    }

    @Override
    public JsonResult addComment(Comment comment) {
        if (comment.getUserId()==0 && comment.getTeacherId()!=0){ //教师评论
            comment.setUserId(null);
            comment.setUserNickname(null);
            List<User> teachers =userMapper.selectById(comment.getTeacherId());
            comment.setTeacherName(teachers.get(0).getTrueName());
            comment.setAvatarUrl(teachers.get(0).getAvatarUrl());
        } else if (comment.getUserId()!=0 && comment.getTeacherId()==0){ //学生评论
            comment.setTeacherId(null);
            comment.setTeacherName(null);
            List<User> users = userMapper.selectById(comment.getUserId());
            comment.setUserNickname(users.get(0).getNickname());
            comment.setAvatarUrl(users.get(0).getAvatarUrl());
        }
//        Date date = new Date();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        sdf.format(date);
//        System.out.println(sdf.format(date));
//        comment.setCreateTime(date);
        Long rows = commentMapper.insertComment(comment);
        if (rows<=0){
            return new JsonResult(1,"添加失败，请联系管理员",null);
        }
        return new JsonResult(0,"添加成功",comment);
    }

    @Override
    public JsonResult deleteComment(Comment comment) {
        // 使用队列实现广度优先搜索删除评论及其子评论
        Queue<Comment> queue = new LinkedList<>();
        queue.offer(comment);
        // 循环处理队列中的评论
        while(!queue.isEmpty()) {
            Comment cur = queue.poll();
            // 删除当前评论
            Long resultNum = commentMapper.deleteById(cur.getId());
            // 删除失败时返回失败的JsonResult对象
            if(resultNum <= 0) return new JsonResult(1,"删除失败",false);
            // 处理当前评论的子评论
            if(cur.getChild() != null) {
                List<Comment> child = cur.getChild();
                for(Comment tmp: child)
                    // 将子评论加入队列，以便继续删除其子评论
                    queue.offer(tmp);
            }
        }
        return new JsonResult(0,"删除成功",true);
    }

    @Override
    public JsonResult selectTextComments(Long id) {
        List<Comment> comments = commentMapper.selectTextComments(id);
        if (comments.size()<=0){
            return new JsonResult(0,"评论为空",null);
        }
        return new JsonResult(0,"查询评论成功",CommentConfig.processComments(comments));
    }
}
